package com.zx.service.specification.impl;

import com.zx.dao.goods.GoodsImgMapper;
import com.zx.dao.specification.SpecificationValueMapper;
import com.zx.dto.request.specification.SpecificationReqDto;
import com.zx.dto.response.goods.GoodsDto;
import com.zx.dto.response.goods.GoodsImgDto;
import com.zx.dto.response.goods.GoodsListDtoResult;
import com.zx.enums.GoodsImgTypeEnums;
import com.zx.service.goods.impl.GoodsImgServiceImpl;
import com.zx.service.specification.SpecificationValueService;
import com.zx.util.Page;
import com.zx.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunkai
 * @date 2022/8/8 0008 15:18
 * @mood zhou xun
 */
@Service
public class SpecificationValueServiceImpl implements SpecificationValueService {
    @Autowired
    private SpecificationValueMapper specificationValueMapper;

    @Autowired
    private GoodsImgMapper goodsImgMapper;

    @Override
    public GoodsListDtoResult querySpecificationValue(SpecificationReqDto specificationReqDto) {
        Integer index=specificationReqDto.getPageIndex();
        specificationReqDto.setPageIndex(
                        PageUtil.pageNumber(specificationReqDto.getPageIndex(),specificationReqDto.getPageSize()));
        List<GoodsDto> goodsDtoList = specificationValueMapper.querySpecificationValue(specificationReqDto);
        GoodsListDtoResult goodsListDtoResult= goodsListPage(specificationReqDto);
        goodsListDtoResult.setPageIndex(index);
        if (ObjectUtils.isEmpty(goodsListDtoResult.getTotalRows())){
            return goodsListDtoResult;
        }
        List<Integer> integerList=goodsDtoList.stream().map(goodsDto->goodsDto.getId()).collect(Collectors.toList());
        Map<Integer,GoodsDto> map=goodsDtoList.stream().collect(Collectors.toMap(GoodsDto::getId,a->a,(k1,k2)->k1));
        if (CollectionUtils.isEmpty(integerList)){
            return goodsListDtoResult;
        }
        List<GoodsDto> goodsDtoList1 = goodImg(integerList, map, goodsDtoList);
        goodsListDtoResult.setGoodsDtoList(goodsDtoList1);
        return goodsListDtoResult;
    }
    private GoodsListDtoResult goodsListPage(SpecificationReqDto specificationReqDto){
        GoodsListDtoResult goodsListDtoResult=new GoodsListDtoResult();
        goodsListDtoResult.setPageSize(specificationReqDto.getPageSize());
        goodsListDtoResult.setTotalRows(specificationValueMapper.sum(specificationReqDto));
        goodsListDtoResult.setTotalPage(
                PageUtil.totalPage(goodsListDtoResult.getTotalRows(),specificationReqDto.getPageSize()));
        return goodsListDtoResult;
    }
    private List<GoodsDto> goodImg(List<Integer> integerList, Map<Integer,GoodsDto> map,List<GoodsDto> goodsDtoList){
        List<GoodsImgDto> goodsImgDtoList = goodsImgMapper.queryList(integerList, GoodsImgTypeEnums.COVER.getType());
        for(GoodsImgDto imgDto:goodsImgDtoList){
            GoodsDto goodsDto = map.get(imgDto.getGoodsId());
            if (CollectionUtils.isEmpty(goodsDto.getGoodsImgDtoList())){
                goodsDto.setGoodsImgDtoList(new ArrayList<>());
            }
            goodsDto.getGoodsImgDtoList().add(imgDto);
        }
        return goodsDtoList;
    }
}
